﻿package com.gengine.resource.loader
{
    import com.gengine.gc.*;
    import com.gengine.resource.core.*;
    import com.gengine.resource.encryption.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;

    public class ABCLoader extends ImageLoader
    {
        private var _bytesInfo:EncryptionInfo;
        private var _jpgBitmapData:BitmapData;
        private static const _point:Point = new Point();

        public function ABCLoader()
        {
            return;
        }// end function

        override public function getClass() : Class
        {
            return ABCLoader;
        }// end function

        override public function initialize(param1) : void
        {
            this._bytesInfo = EncryptUtils.decodeStream(param1);
            if (this._bytesInfo.contentType == EncryptUtils.JPG || this._bytesInfo.contentType == EncryptUtils.PNG)
            {
                super.initialize(this._bytesInfo.contentBytes);
            }
            else if (this._bytesInfo.contentType == EncryptUtils.MEGER)
            {
                StreamManager.pushIn(this);
            }
            else
            {
                var _loc_2:* = this._bytesInfo;
                resourceInfo.data = this._bytesInfo;
                this.onLoadCompleteHandler();
            }
            return;
        }// end function

        override public function loadByteArray() : void
        {
            if (this._bytesInfo.contentType == EncryptUtils.JPG || this._bytesInfo.contentType == EncryptUtils.PNG)
            {
                super.loadByteArray();
            }
            else if (this._bytesInfo.contentType == EncryptUtils.MEGER)
            {
                this.loadBytesImg(this._bytesInfo.contentBytes, this.loadJpgCompleteHander);
            }
            return;
        }// end function

        override protected function onLoadCompleteHandler() : void
        {
            if (this._bytesInfo)
            {
                if (this._bytesInfo.contentType == EncryptUtils.MEGER)
                {
                    GCPool.instance.gcByteArray(this._bytesInfo.otherBytes);
                    GCPool.instance.gcByteArray(this._bytesInfo.contentBytes);
                }
            }
            if (this._jpgBitmapData)
            {
                GCPool.instance.gcBitmapdata(this._jpgBitmapData);
                this._jpgBitmapData = null;
            }
            this._bytesInfo = null;
            super.onLoadCompleteHandler();
            return;
        }// end function

        private function loadJpgCompleteHander(event:Event) : void
        {
            var _loc_2:* = event.currentTarget as LoaderInfo;
            _loc_2.removeEventListener(Event.COMPLETE, this.loadJpgCompleteHander);
            _loc_2.removeEventListener(IOErrorEvent.IO_ERROR, onChangeIOErrorHandler);
            _loc_2.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onChangeIOErrorHandler);
            this._jpgBitmapData = Bitmap(_loc_2.content).bitmapData;
            this.loadBytesImg(this._bytesInfo.otherBytes, this.loadPngCompleteHander);
            return;
        }// end function

        private function loadPngCompleteHander(event:Event) : void
        {
            var _loc_4:BitmapData = null;
            var _loc_2:* = event.currentTarget as LoaderInfo;
            _loc_2.removeEventListener(Event.COMPLETE, this.loadPngCompleteHander);
            _loc_2.removeEventListener(IOErrorEvent.IO_ERROR, onChangeIOErrorHandler);
            _loc_2.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onChangeIOErrorHandler);
            var _loc_3:* = Bitmap(_loc_2.content).bitmapData;
            if (resourceInfo)
            {
                _loc_4 = new BitmapData(this._jpgBitmapData.width, this._jpgBitmapData.height, true, 0);
                _loc_4.copyPixels(this._jpgBitmapData, this._jpgBitmapData.rect, _point);
                _loc_4.copyChannel(_loc_3, this._jpgBitmapData.rect, _point, BitmapDataChannel.RED, BitmapDataChannel.ALPHA);
                resourceInfo.data = _loc_4;
            }
            GCPool.instance.gcBitmapdata(_loc_3);
            this.onLoadCompleteHandler();
            return;
        }// end function

        private function loadBytesImg(param1:ByteArray, param2:Function) : void
        {
            var _loc_3:* = new Loader();
            _loc_3.contentLoaderInfo.addEventListener(Event.COMPLETE, param2);
            _loc_3.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onChangeIOErrorHandler);
            _loc_3.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onChangeIOErrorHandler);
            _loc_3.loadBytes(param1);
            return;
        }// end function

        override protected function onContentReady(param1) : Boolean
        {
            if (resourceInfo && loaderInfo)
            {
                resourceInfo.data = loaderInfo;
            }
            return true;
        }// end function

    }
}
